package com.kartoshka.events 
{
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	import flash.utils.IExternalizable;
	import flash.events.Event;

	/**
	 * @author Alexander Zats, 2009
	 */
	 
	/*
	 * KartoshkaDataEvent implements IExternalizable interface, which means you can send this event on wire
	 * One thing you should remember about dispatching this event on other end: since <code>Event.type</code> 
	 * is readonly variable by default you should call event.clone() before you'll try to dispatch it.
	 */
	public class KartoshkaDataEvent extends Event implements IExternalizable
	{

		private var _data : Object;
		private var _type : String;
		private var _bubbles : Boolean;
		private var _cancelable : Boolean;

		public function KartoshkaDataEvent( type : String = "", data : Object = null, bubbles : Boolean = false, cancelable : Boolean = false )
		{
			super( type ? type : "placebo", bubbles, cancelable );
			_data = data;
			_type = type;
			_bubbles = bubbles;
			_cancelable = cancelable;
		}

		public function readExternal(input : IDataInput) : void
		{
			_type = input.readUTF( );
			_data = input.readObject( );
			_bubbles = input.readBoolean( );
			_cancelable = input.readBoolean( );
		}

		public function writeExternal(output : IDataOutput) : void
		{
			output.writeUTF( _type );
			output.writeObject( _data );
			output.writeBoolean( _bubbles );
			output.writeBoolean( _cancelable );
		}

		public function get data() : Object
		{
			return _data;
		}

		public function set data( value : Object ) : void
		{
			_data = value;
		}

		override public function get type() : String
		{
			return _type;
		}

		public function set type(value : String) : void
		{
			_type = value;
		}

		override public function get bubbles() : Boolean
		{
			return _bubbles;
		}

		public function set bubbles(value : Boolean) : void
		{
			_bubbles = value;
		}

		override public function get cancelable() : Boolean
		{
			return _cancelable;
		}

		public function set cancelable(value : Boolean) : void
		{
			_cancelable = value;
		}

		override public function clone() : Event
		{
			return new KartoshkaDataEvent( _type, _data, _bubbles, _cancelable );
		}

		override public function toString() : String
		{
			return "[KartoshkaDataEvent ( type=" + _type + ", data=" + _data + " )]";
		}
	}
}
